package com.lipei.vrp.imp;

import java.util.ArrayList;

import com.lipei.vrp.IAlgorithm;
import com.lipei.vrp.IAlgorithmCreator;
import com.lipei.vrp.Opt;
import com.lipei.vrp.ResultType;

public class VRPH implements IAlgorithmCreator {

	private class Impl implements IAlgorithm {
		public Impl(String[] args) {
		}

		@Override
		public Opt parse(ArrayList<String> list, boolean timeout, boolean failed) {
			ResultType resulttype = null;
			if (timeout) {
				resulttype = ResultType.OT;
			}

			int i = list.size() - 1;
			for (; i >= 0; i--) {
				String line = list.get(i);
				if (line.contains("Solution Cost")) {
					break;
				}
			}

			Opt opt;
			if (i >= 0) {
				ArrayList<String> sublines = new ArrayList<String>();
				int cost = i;
				i += 2;

				for (; i < list.size(); i++) {
					sublines.add(list.get(i));
				}
				opt = Opt.readroute(sublines);
				opt.resulttype = resulttype;
				String costline = list.get(cost);
				opt.cost = Double.parseDouble(costline.substring(15));
			} else {
				opt = new Opt();
				opt.resulttype = resulttype;
			}

			return opt;
		}

		@Override
		public ArrayList<String> generateCommand(String vrppath) {
			return null;
		}
	}

	@Override
	public IAlgorithm argsCheck(String[] args) {
		return new Impl(args);
	}

	public static IAlgorithmCreator createIns() {
		// TODO Auto-generated method stub
		return new VRPH();
	}

}
